package cn.tedu._14vehicle703.controller;

import cn.tedu._14vehicle703.base.response.JsonResult;
import cn.tedu._14vehicle703.base.response.StatusCode;
import cn.tedu._14vehicle703.mappper.UserMapper;
import cn.tedu._14vehicle703.pojo.dto.UserRegParam;
import cn.tedu._14vehicle703.pojo.entity.User;
import cn.tedu._14vehicle703.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@Slf4j
@Validated
@RestController
@Tag(name="用户管理模块")
@RequestMapping("v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    /**
     * 1.用户注册功能
     */
    @Operation(summary = "用户注册")
    @ApiOperationSupport(order = 10)
    @PostMapping("reg")
    @Parameters(value = {
            @Parameter(name = "userRegParam",hidden = true),
            @Parameter(name = "username",description = "用户名",required = true),
            @Parameter(name = "password",description = "密码",required = true),
            @Parameter(name = "email",description = "邮箱",required = true),
            @Parameter(name = "phone",description = "手机号码",required = true)
    })
    public JsonResult reg(@Validated UserRegParam userRegParam){
        log.debug("userRegParam = "+userRegParam);
        /**
         * 1.接收客户端传入的参数
         * 2.校验数据的合法性【暂时不处理】
         * 3.校验用户名是否被占用【SELECT接口】
         */
        String username = userRegParam.getUsername();
        int count = userMapper.selectUserByUsername(username);
        if (count > 0){
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.insertUser(user);
        return JsonResult.ok();
    }
    /**
     * 2.用户登录功能
     */
    @Operation(summary = "用户登录")
    @ApiOperationSupport(order = 20)
    @PostMapping("login")
    @Parameters(value = {
            @Parameter(name = "userRegParam",hidden = true),
            @Parameter(name = "username",description = "用户名",required = true),
            @Parameter(name = "password",description = "密码",required = true),
            @Parameter(name = "email",hidden = true),
            @Parameter(name = "phone",hidden = true)
    })
    public JsonResult login(UserRegParam userRegParam, HttpSession session){
        log.debug("userRegParam = " + userRegParam);
        UserVO userVO = userMapper.selectByUsernameAndPassword(userRegParam);
//        if (userVO == null){
//            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
//        }
        //将用户信息保存到session中
        session.setAttribute("user", userVO);
        return JsonResult.ok(userVO);
    }
    /**
     * 3.获取用户信息
     */
    @Operation(summary = "获取用户信息功能")
    @ApiOperationSupport(order = 30)
    @GetMapping("currentUser")
    public JsonResult currentUser(HttpSession session) {
        //服务器内存中获取当前登录用户信息
        UserVO userVO = (UserVO) session.getAttribute("user");
        return JsonResult.ok(userVO);
    }
    /**
     * 4.退出登录
     */
    @Operation(summary = "退出登录功能")
    @ApiOperationSupport(order = 40)
    @GetMapping("logout")
    public JsonResult logout(HttpSession session) {
        //删除服务器内存中的当前登录用户信息
        session.removeAttribute("user");
        return JsonResult.ok();
    }
}
